# -*- coding: utf-8 -*-
# @Time    : 2024/1/17  16:45
# @Author  : zhanghao
# @FileName: file_result.py
# @Software: PyCharm
"""
    Description: 文件执行结果
"""
from pydantic import BaseModel
from typing import Optional

import json


class FileResult(BaseModel):

    id: int
    file_id: str
    file_type: str
    file_link: str
    # 0: 待执行 1: 识别中 2: 成功 3: 失败
    task_status: int
    # 0: 待发送 1: 已发送
    res_send: int
    file_md5: str
    task_result: str
    reason: Optional[str]
    info: Optional[str]

    @staticmethod
    def buildBlank():
        result = FileResult(id=-1, file_id='', file_type='', file_link='', task_status=0, res_send=0,
                            file_md5='', task_result='', reason='', info='')

        return result

    @staticmethod
    def build(id, file_id, file_type, file_link, task_status, res_send, file_md5, task_result, reason, info):
        result = FileResult(id=id, file_id=file_id, file_type=file_type, file_link=file_link,
                            task_status=task_status, res_send=res_send, file_md5=file_md5,
                            task_result=task_result, reason=reason, info=info)

        return result

    @staticmethod
    def fetchTaskStatus(task_status):
        desc = ''
        if task_status == 0:
            desc = '待处理'
        elif task_status == 1:
            desc = '识别中'
        elif task_status == 2:
            desc = '成功'
        elif task_status == 3:
            desc = '失败'

        return desc

    @staticmethod
    def fetchSendStatus(res_send):
        desc = ''
        if res_send == 0:
            desc = '待发送'
        elif res_send == 1:
            desc = '已发送'

        return desc

    def appendInfo(self, info_key, info_value):
        json_obj = json.loads(self.info)
        if info_key == 'first_execute_time' and info_key not in json_obj:
            json_obj[info_key] = info_value
        elif info_key != 'first_execute_time':
            json_obj[info_key] = info_value
        self.info = json.dumps(json_obj)

    def fetchInfo(self, info_key):
        result = ''
        json_obj = json.loads(self.info)
        if info_key and info_key in json_obj:
            result = json_obj[info_key]

        return result

